package org.lmw.tools.kd;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.lmw.tools.kd.entity.KDLog;

import android.app.Application;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class APP extends Application {
	
	
	public static SharedPreferences sp;
	public static Editor editor;
	public static List<KDLog> logs=new ArrayList<KDLog>();
	Type type=new TypeToken<List<KDLog>>(){}.getType();
	@Override
	public void onCreate() {
		super.onCreate();
		sp=getSharedPreferences("log", MODE_PRIVATE);
		editor=sp.edit();
		
		List<KDLog> list=new Gson().fromJson(sp.getString("logs", ""), type);
		if(null!=list){
			logs.addAll(list);	
		}
	}
	
	public static void addLog(KDLog log){
		boolean contains=false;
		for(KDLog l:logs){
			if(l.getExpressId().equals(log.getExpressId())){
				contains=true;
			break;	
			}
		}
		
		if(!contains){
			logs.add(log);
			editor.putString("logs", new Gson().toJson(logs));
			editor.commit();
		}
	}
	
	public static void removeLog(KDLog log){
		logs.remove(log);
		editor.putString("logs", new Gson().toJson(logs));
		editor.commit();
	}
	
}
